//
// Created by Administrator on 2022/6/15.
//

#ifndef RAPIDSVN_WIN_repDialog_H
#define RAPIDSVN_WIN_repDialog_H

#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <wx/dialog.h>
#include <wx/sizer.h>
#include <wx/button.h>
#include <wx/statline.h>
#include <wx/stattext.h>
#include <wx/listbox.h>
#include <wx/filepicker.h>
#include <mysql.h>
#include <vector>
#include <func.h>
#include "libssh2.h"

using namespace std;

class repDialog : public wxFrame
{
    class rscItemPanel: public wxPanel {
    public:
        enum ItemType{
            thunder, mule
        };
        rscItemPanel( repDialog* d, wxWindow* parent, const wxString& dir, const wxString& item, ItemType type );
        ItemType type;
        wxString dir;
        wxString item;
        wxGauge* g;
        wxComboBox* t;
        wxButton * b;
        repDialog* dialog;
    };

    /*
     * 用线程建立一个不可见对话框，然后又能接受消息，又能用线程办事
     */
    class PostFileWorker: public wxThread{
        MYSQL *conn_ptr;
        LIBSSH2_SESSION *session;
        int sock;
        int insertTorrent( const wxString &dir, const wxString &torrent_path );
        int RunSql( const wxString& sql,  char * msg );
        int RunSql( const char* sql,  char * msg );
        int scp(  const wxString &src, const wxString& dest );
        int scp(  const char* src, libssh2_int64_t size,
                  time_t modified, const char* dest );
        int ssh_cmd( const char* cmd , char* response );
        int ssh_cmd( const wxString& cmd , char* response );
        void pingMySql();
        void pingSsh();
        repDialog* dialog;
        void addLog( const wxString &log );
        bool lost_connection;
        int process_partial_block( LIBSSH2_CHANNEL* channel, char* buffer, size_t sum );
        int scp_process_block( LIBSSH2_CHANNEL *channel, const char *buffer, size_t nRead );
    public:
        ~PostFileWorker() override;
        //添加和删除元素时加锁
        wxArrayString srcs;
        explicit PostFileWorker ( repDialog* dialog1 );
        void * Entry() override;
    };
    wxBoxSizer *scroll_window_sizer;
public:
    static repDialog* getInstance( wxWindow *parent ){
        if( dialog== nullptr ){
            dialog = new repDialog( parent );
        }
        return dialog;
    }
    ~repDialog() override;

    //void OnPostAll( wxCommandEvent& event );
    void addLog( const wxString& log );
    void remove_panel ( rscItemPanel * panel );
private:
    static repDialog* dialog;
    explicit repDialog( wxWindow *parent = nullptr );
    wxVector<rscItemPanel*> panels;

    void buildMenu();
    void OnExit(wxCommandEvent& event);

    void buildList();

    wxPanel *m_panel;
    wxBoxSizer *dialogRootBoxSizer;
    wxScrolledWindow* m_listbox;
    wxTextCtrl* m_logBox;

    void OnActionEvent(wxCommandEvent &event);
    void parseArguments();
    PostFileWorker *postFileWorker;
    void update_percent( const wxString &dir, int percent );
DECLARE_EVENT_TABLE()
};


#endif //RAPIDSVN_WIN_repDialog_H
